<template>
  <a-button @click="onClick" type="primary" :disabled="disabled">
    下 载
  </a-button>
  <!-- :disabled="disabled" -->
</template>
<script setup>
import { ref, computed } from 'vue'
import {
  useRowSelectionActive,
  useRowSelection,
  useDataSource,
  // useTreeSelectedInfo,
  useLockForInUse,
  useDownloadBarVisible,
} from './provide-context.js'
/**
 * 全局
 */
let dataSource = useDataSource()
// let treeSelectedInfo = useTreeSelectedInfo()
let rowSelectionActive = useRowSelectionActive()
let rowSelection = useRowSelection()
let lockForInUse = useLockForInUse()
let downloadBarVisible = useDownloadBarVisible()
/**
 * 锁标识
 */
let lockSymbol = Symbol('download')
/**
 * 事件
 */
function onClick() {
  if (rowSelectionActive.value) {
    lockForInUse.value = null
    delete rowSelection.getCheckboxProps
  } else {
    lockForInUse.value = lockSymbol
    rowSelection.getCheckboxProps = record => ({
      disabled:
        record.dataType === 'folder' ||
        record?.permissions?.download === false,
    })
  }
  downloadBarVisible.value = !downloadBarVisible.value
  rowSelectionActive.value = !rowSelectionActive.value
  rowSelection.selectedRowKeys.value = []
}
/**
 * 是否在创建中
 */
let createing = computed(() =>
  dataSource.value.some(({ editing }) => editing === true),
)
let disabled = computed(() => {
  return (
    // !treeSelectedInfo.value ||
    // (treeSelectedInfo.value.children || []).length > 0 ||
    dataSource.value.length === 0 ||
    createing.value ||
    (lockForInUse.value && lockForInUse.value !== lockSymbol)
  )
})
</script>
